export function formatNumber(n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}
export function log(data) {
  return console.log(data)
}
export function validatePhoneNumber(data) {
  const reg = new RegExp('^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$')
  return reg.test(data)
}
export function validateEmail(data) {
  const reg = new RegExp('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*.[a-zA-Z0-9]{2,6}$')
  return reg.test(data)
}
export function validateForm(form, value, that) {
  that.validate = true
  form.forEach(item => {
    if (item.type === '2') {
      value['para' + item.id] = item.list[value['para' + item.id]].value
    }
    if (item.wr_ok === '1' && that.validate) {
      const input = value['para' + item.id]
      switch (item.type) {
        case '4':
          if (input.length > 0) {
            that.validate = true
          } else {
            that.Message({
              content: item.name + '不能为空',
              type: 'error'
            })
            that.validate = false
          }
          break
        case '8':
          if (input !== '') {
            if (validatePhoneNumber(input)) {
              that.validate = true
            } else {
              that.Message({
                content: '请输入正确的电话号码',
                type: 'error'
              })
              that.validate = false
            }
          } else {
            that.Message({
              content: item.name + '不能为空',
              type: 'error'
            })
            that.validate = false
          }
          break
        case '9':
          if (input !== '') {
            if (validateEmail(input)) {
              that.validate = true
            } else {
              that.Message({
                content: '请输入正确的邮箱地址',
                type: 'error'
              })
              that.validate = false
            }
          } else {
            that.Message({
              content: item.name + '不能为空',
              type: 'error'
            })
            that.validate = false
          }
          break

        default:
          if (input !== '') {
            that.validate = true
          } else {
            that.Message({
              content: item.name + '不能为空',
              type: 'error'
            })
            that.validate = false
          }
          break
      }
    }
  })
  return that.validate
}
export function navigateTo(data) {
  wx.navigateTo({
    url: data
  })
}
export function switchTab(type, that) {
  const param = {
    column_id: that.data.column_id,
    module: that.data.module,
    classtype: that.data.classtype
  }
  let setName
  switch (type) {
    case 'product':
      setName = 'SetProductColumn'
      break
    case 'news':
      setName = 'SetNewsColumn'
      break
    case 'about':
      setName = 'SetAboutColumn'
      break
  }
  that.$store.dispatch('GetSubcolumnNav', param)
  that.$store.dispatch(setName, param)
  const url = '../../pages/' + type + '/main'
  wx.switchTab({
    url: url
  })
}
export function navigateToViewPage(type, val, that) {
  let funName, url
  switch (type) {
    case 'product':
      funName = 'GetProductView'
      url = '../../pages/showproduct/main'
      break
    case 'news':
      funName = 'GetNewsView'
      url = '../../pages/shownews/main'
      break
  }
  const param = {
    id: val.id,
    module: val.module
  }
  that.$store.dispatch(funName, param).then(response => {})
  const data = {
    module: val.module
  }
  that.$store.dispatch('GetSidebar', data).then(response => {
    that.$navigateTo(url)
  })
}

export function navigateToListPage(val, that, pick) {
  let param, funName, setName
  param = {
    column_id: val.id,
    module: val.module,
    classtype: val.classtype,
    name: val.name,
    bigmodule: val.bigmodule,
    bigclass: val.bigclass && pick && val.classtype === '3' ? val.bigclass : val.id
  }
  that.$store.dispatch('GetBanner', param)
  that.$store.dispatch('SetActiveColumn', param)
  if (val.bigmodule) {
    switch (val.bigmodule) {
      case '3':
        setName = 'SetProductColumn'
        break
      case '2':
        setName = 'SetNewsColumn'
        break
      case '1':
        setName = 'SetAboutColumn'
        break
    }
  } else {
    switch (val.module) {
      case '3':
        setName = 'SetProductColumn'
        break
      case '2':
        setName = 'SetNewsColumn'
        break
      case '1':
        setName = 'SetAboutColumn'
        break
    }
  }
  switch (val.module) {
    case '3':
      funName = 'GetProductList'
      break
    case '2':
      funName = 'GetNewsList'
      break
    case '1':
      funName = 'GetAboutList'
      break
    case '7':
      funName = 'GetMessageList'
      break
    case '8':
      funName = 'GetFeedbackList'
      break
  }

  wx.showLoading({
    title: '加载中'
  })
  that.$store.dispatch(setName, param)
 
  that.$store.dispatch(funName, param).then(response => {
    if (response.data.page) {
      const data = {
        page: response.data.page,
        page_num: response.data.page_num,
        is_more: response.data.page_num > response.data.page ? true : false
      }
      that.$emit('resetPage', data)
    }
    wx.hideLoading()
  })
}

export function fetchListPage(val, that) {
  wx.setNavigationBarTitle({
    title: that.$store.getters.app_webName
  })
  const app_tabBar = that.$store.getters.app_tabBar
  let funName, active
  const type = val.bigmodule ? val.bigmodule : val.module
  switch (type) {
    case '3':
      funName = 'GetProductList'
      active = app_tabBar.product
      break
    case '2':
      funName = 'GetNewsList'
      active = app_tabBar.news
      break
    case '1':
      funName = 'GetAboutList'
      active = app_tabBar.about
      break
  }
  that.$store.dispatch('SetActiveColumn', val)
  that.$store.dispatch('GetSubcolumnNav', active).then(response => {})
  that.$store.dispatch(funName, val).then(response => {
    if (response.data.style) {
      that.config.style = response.data.style
    }
    if (response.data.page) {
      const data = {
        page: response.data.page,
        page_num: response.data.page_num,
        is_more: response.data.page_num > response.data.page ? true : false
      }
      that.page_config = data
    }
    setTimeout(() => {
      that.loading = false
    }, 500)
  })
  const param = {
    column_id: that.column.active.column_id
  }
  that.$store.dispatch('GetBanner', param).then(response => {})
  setTimeout(() => {
    that.loading = false
  }, 500)
}

export function getMoreList(type, that) {
  if (that.page_config.is_more) {
    that.isLoad = false
    wx.showLoading({
      title: '加载中'
    })
    let funName
    if (type === 'product') {
      funName = 'GetProductList'
    } else if (type === 'news') {
      funName = 'GetNewsList'
    }
    const param = {
      column_id: that.column.active.column_id,
      classtype: that.column.active.classtype,
      module: that.column.active.module,
      page: that.page_config.page + 1
    }
    that.$store.dispatch(funName, param).then(response => {
      const data = {
        page: response.data.page,
        page_num: response.data.page_num,
        is_more: response.data.page_num > response.data.page ? true : false
      }
      that.page_config = data
      setTimeout(() => {
        wx.hideLoading()
        that.isLoad = true
      }, 500)
    })
  }
}

/*
*  将对象转变为class字符串
*/
export function classes(obj) {
  let classesStr = ''
  for (let className in obj) {
    if (obj.hasOwnProperty(className) && !!obj[className]) classesStr += className
  }
  return classesStr
}

/*
* 将对象转变为style字符串，对象属性的camel命名会转换为中划线命名
*/
export function styles(obj) {
  let stylesStr = ''
  for (let styleName in obj) {
    if (obj.hasOwnProperty(styleName))
      stylesStr += `${styleName.replace(/([A-Z])/g, '-$1').toLowerCase()}:${px2rpx(obj[styleName])};`
  }
  return stylesStr
}

export function px2rpx(str) {
  if (/rpx/.test(str)) return str
  else if (/px/.test(str)) {
    let val = str.substring(0, str.indexOf('px'))
    return `${(val - 0) * 2}rpx`
  } else return str
}
